package com.briup.day38;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

public class IOTest {
	
	static String path = "src/com/briup/day38/demo5.txt";
	static int bufferSize = 1024;

	public static void main(String[] args) throws Exception  {
		
		long t1 = System.currentTimeMillis();
		read1();
		long t2 = System.currentTimeMillis();
		read2();
		long t3 = System.currentTimeMillis();
		read3();
		long t4 = System.currentTimeMillis();
		read4();
		long t5 = System.currentTimeMillis();
		
		System.out.println("\nread1读取完成" + "共耗时" + (t2 - t1) + "毫秒。");
		System.out.println("read2读取完成" + "共耗时" + (t3 - t2) + "毫秒。");
		System.out.println("read3读取完成" + "共耗时" + (t4 - t3) + "毫秒。");
		System.out.println("read4读取完成" + "共耗时" + (t5 - t4) + "毫秒。");
		

	}


	public static void read1() throws Exception {
		InputStream is = new FileInputStream(path);
		//每次读取一个字节
		int i = -1;
		while ((i = is.read()) != -1) {
			System.out.print((char) i);
		}
	}
	
	public static void read2() throws Exception {
		InputStream is = new FileInputStream(path);
		//每次读取一个字节
		byte[] buf = new byte[bufferSize];
		int len = -1;
		while ((len = is.read(buf)) != -1) {
			System.out.print(new String(buf,0,len));
		}
	}
	
	private static void read3() throws Exception {
		InputStream is = new FileInputStream(path);
		BufferedInputStream bis = new BufferedInputStream(is);
		//每次读取一个字节
		int i = -1;
		while ((i = bis.read()) != -1) {
			System.out.print((char) i);
		}
		
	}
	
	private static void read4() throws Exception {
		InputStream is = new FileInputStream(path);
		BufferedInputStream bis = new BufferedInputStream(is);
		//每次读取一个字节
		byte[] buf = new byte[bufferSize];
		int len = -1;
		while ((len = bis.read(buf)) != -1) {
			System.out.print(new String(buf,0,len));
		}
		
	}

}
